package com.bristua.user.biz;

import com.bristua.common.bean.PageBean;
import com.bristua.user.dao.TUserReturnIntegralFlowMapper;
import com.bristua.user.model.TUserCashTake;
import com.bristua.user.model.TUserReturnIntegralFlow;
import com.bristua.user.model.condition.QueryUserReturnIntegralParams;
import com.bristua.user.model.condition.TUserReturnIntegralFlowExample;
import com.bristua.user.model.vo.QueryUserReturnIntegralVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class QueryUserReturnIntegralBiz {

    @Resource
    private TUserReturnIntegralFlowMapper tUserReturnIntegralFlowMapper;

    public PageBean queryUserReturnIntegral(QueryUserReturnIntegralParams queryUserReturnIntegralParams){
        TUserReturnIntegralFlowExample tUserReturnIntegralFlowExample = new TUserReturnIntegralFlowExample();
        tUserReturnIntegralFlowExample.setOrderByClause("create_time desc");
        TUserReturnIntegralFlowExample.Criteria tUserReturnIntegralFlowExampleCriteria = tUserReturnIntegralFlowExample.createCriteria();

        if(!StringUtils.isEmpty(queryUserReturnIntegralParams.getStartTime())){
            tUserReturnIntegralFlowExampleCriteria.andCreateTimeGreaterThan(queryUserReturnIntegralParams.getStartTime()+" 00:00:00");
        }

        if(!StringUtils.isEmpty(queryUserReturnIntegralParams.getEndTime())){
            tUserReturnIntegralFlowExampleCriteria.andCreateTimeLessThan(queryUserReturnIntegralParams.getEndTime()+" 23:59:59");
        }

        Page<TUserCashTake> objects = PageHelper.startPage(queryUserReturnIntegralParams.getPageNo(),queryUserReturnIntegralParams.getPageSize());
        tUserReturnIntegralFlowMapper.selectByExample(tUserReturnIntegralFlowExample);
        PageBean data = new PageBean<>(objects);
        if(data.getList()!=null && data.getList().size()>0){
            List<QueryUserReturnIntegralVO> queryUserReturnIntegralVOS = new ArrayList<>();
            for(int i=0;i<data.getList().size();i++){
                TUserReturnIntegralFlow tUserReturnIntegralFlow = (TUserReturnIntegralFlow) data.getList().get(i);
                QueryUserReturnIntegralVO queryUserReturnIntegralVO = new QueryUserReturnIntegralVO();
                queryUserReturnIntegralVO.setCreateTime(tUserReturnIntegralFlow.getCreateTime());
                queryUserReturnIntegralVO.setReturnIntegral(tUserReturnIntegralFlow.getReturnIntegral()==null?"0":tUserReturnIntegralFlow.getReturnIntegral()+"");
                queryUserReturnIntegralVO.setUserName(tUserReturnIntegralFlow.getReceiveUserName());
                queryUserReturnIntegralVO.setUserPointId(tUserReturnIntegralFlow.getUserPointId());
                queryUserReturnIntegralVOS.add(queryUserReturnIntegralVO);
            }
            data.getList().clear();
            data.getList().addAll(queryUserReturnIntegralVOS);
        }
        return data;
    }
}
